import {
	ossUpload,
	ossDel,
	ossUploadImg
} from '@/common/oss.js'
const config = require('./config')

const FileMAXSIZE = 20 //大小
const MAXSIZE = 200 //大小
const MAXNUM = 9 //
export const upload = (filePath, data = {}) => {
	return new Promise((res, rej) => {
		uni.uploadFile({
			url: config.url + 'uploadImage',
			filePath,
			header: {
				"token": uni.getStorageSync('token')
			},
			name: 'file',
			formData: data,
			success: (uploadFileRes) => {
			
				res(uploadFileRes)
			},
			fail: (res) => {
			
				rej(res)
			}
		})
	})
}

export const uploadImg = (count = 1) => {
	return new Promise((resolve, reject) => {
		uni.chooseImage({
			count,
			success: (res) => {
				const tempFilePaths = res.tempFilePaths;
				uni.getFileInfo({
					filePath: tempFilePaths[0],
					success: (info) => {
						const maxSize = MAXSIZE * 1024 * 1024; // 最大允许图片大小：5MB
						if (info.size > maxSize) {
							let title = '图片大小不能超过' + MAXSIZE + 'MB'
							uni.showToast({duration:3000,
								title,
								icon: 'none'
							})
							reject({
								msg: title,
								data: ''
							})
						} else {
							// this.uploadImage(tempFilePaths[0]);
							console.log('图片大小：', info.size / 1024 / 1024)
							
							ossUpload(tempFilePaths[0]).then(res => {
								resolve({
									msg: '',
									data: res
								})
							}).catch(err => {
								uni.showToast({duration:3000,
									title: '上传失败！',
									icon: 'none'
								})
								reject({
									msg: err,
									data: err
								})
							})
						}
					}
				});
			},
			fail: (err) => {
				console.error('选择图片失败', err);
				uni.showToast({duration:3000,
					title: '选择图片失败',
					icon: 'none'
				})
				reject({
					msg: '选择图片失败',
					data: err
				})
			}
		});
	})
}

export const uploadImgs = (filePath) => {
	return new Promise((resolve, reject) => {
		const maxSize = MAXSIZE * 1024 * 1024; // 最大允许图片大小：5MB
	
		if (filePath.size > maxSize) {
			let title = '图片大小不能超过' + MAXSIZE + 'MB'
			uni.showToast({duration:3000,
				title,
				icon: 'none'
			})
			reject({
				msg: title,
				data: ''
			})
		} else {
			
			let http = filePath.size > 5 * 1024 * 1024 ? ossUploadImg : ossUpload 
			// this.uploadImage(tempFilePaths[0]);
			http(filePath.path).then(res => {
			
				resolve({
					msg: '',
					data: res
				})
			}).catch(err => {
				uni.showToast({duration:3000,
					title: '上传失败！',
					icon: 'none'
				})
				reject({
					msg: err,
					data: err
				})
			})
		}
	})
}

export const uploadVideo = () => {
	return new Promise((resolve, reject) => {
		uni.chooseVideo({
			sourceType: ['album', 'camera'],
			maxDuration: 60,
			success: (res) => {
				console.log('res', res)
				const tempFilePath = res.tempFilePath;
				let duration = res.duration
				uni.getFileInfo({
					filePath: tempFilePath,
					success: (info) => {
						const maxSize = MAXSIZE * 1024 * 1024; // 最大允许图片大小：5MB

						if (info.size > maxSize) {
							let title = '视频大小不能超过' + MAXSIZE + 'MB'
							uni.showToast({duration:3000,
								title,
								icon: 'none'
							})
							reject({
								msg: title,
								data: ''
							})
						} else {
							// this.uploadImage(tempFilePaths[0]);
							ossUpload(tempFilePath).then(res => {
							
								resolve({
									msg: '',
									data: {
										...res,
										duration
									}
								})
							}).catch(err => {
								uni.showToast({duration:3000,
									title: '上传失败！',
									icon: 'none'
								})
								reject({
									msg: err,
									data: err
								})
							})
						}
					}
				});
			},
			fail: (err) => {
				console.error('选择视频失败', err);
				uni.showToast({duration:3000,
					title: '选择视频失败',
					icon: 'none'
				})
				reject({
					msg: '选择视频失败',
					data: err
				})
			}
		});
	})
}

export const uploadDel = (url) => {
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: "删除中"
		})
		ossDel(url).then(res => {
			resolve()
		}).catch(err => {
			uni.showToast({duration:3000,
				title: "删除失败",
				icon: 'none'
			})
			reject()
		}).finally(() => {
			uni.hideLoading()
		})

	})
}

export const uploadFile = () => {
    return new Promise((resolve, reject) => {
        // 选择文件
        wx.chooseMessageFile({
            count: 1, // 默认1
            type: 'file',
            extension: ['pdf'], // 尝试指定扩展名
            success: function(res) {
				console.log('resres', res)
                const tempFilePath = res.tempFiles[0].path;
                const fileName = res.tempFiles[0].name;

                // 验证文件扩展名
                if (!fileName.endsWith('.pdf')) {
                    uni.showToast({duration:3000,
                        title: '请选择PDF文件',
                        icon: 'none'
                    });
                    reject({
                        msg: '请选择PDF文件',
                        data: ''
                    });
                    return;
                }

                // 获取文件信息
                wx.getFileInfo({
                    filePath: tempFilePath,
                    success: (info) => {
                        const maxSize = FileMAXSIZE * 1024 * 1024; // 最大允许文件大小：200MB
                        if (info.size > maxSize) {
                            let title = '文件大小不能超过' + FileMAXSIZE + 'MB';
                           
                            reject({
                                msg: title,
                                data: ''
                            });
                        } else {
                            // 上传文件
                            ossUpload(tempFilePath).then(res => {
                                resolve({
                                    msg: '',
                                    data: {
										name: fileName,
										url: res.url
									}
                                });
                            }).catch(err => {
                                
                                reject({
                                    msg: err,
                                    data: err
                                });
                            });
                        }
                    },
                    fail: (err) => {
                        console.error('获取文件信息失败', err);
                       
                        reject({
                            msg: '获取文件信息失败',
                            data: err
                        });
                    }
                });
            },
            fail: (err) => {
                console.error('选择文件失败', err);
                
                reject({
                    msg: '选择文件失败',
                    data: err
                });
            }
        });
    });
}